Home:ALL Converter>How to run in wee8 wasm code that was compiled from c++ with emcc? (WASI in wee8?)

How to run in wee8 wasm code that was compiled from c++ with emcc? (WASI in wee8?)

Ask Time:2022-03-15T00:55:44         Author:talz

Json Formatter

I am trying to compile C++ code to wasm and then embed it in other C++ code with wee8 (v8's wasm-api). Currently I'm getting a Segfault on instantiating the module:

    auto instance = wasm::Instance::make(store, module.get(), imports);

Note that I have no problem embedding code that I write as .wat and convert to .wasm, so the problem is specifically with embedding code compiled with emcc. I am guessing that what I'm missing is WASI support in wee8? Does it exist? How can I enable it? Alternatively: can I ask emcc not to generate any WASI calls?

Here is a minimal example which results in:

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

In cpp42.cpp:

int main() {
    return 42;
}

Compiling this to wasm with:

emcc -O3 cpp42.cpp -o cpp42.wasm

Inspecting the compiled wasm module with wabt's wasm2wat shows that it contains the following import

  (import "wasi_snapshot_preview1" "proc_exit" (func (;0;) (type 0)))

Which I suspect to be the cause of the problem.

Then embedding with wee8 like in the examples in the repo and like I do with other wasm files causes the segfault mentioned above.

Just as another check: running

wasmer cpp42.wasm 
echo $?
> 42

Works without a problem.

Author:talz,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/71471462/how-to-run-in-wee8-wasm-code-that-was-compiled-from-c-with-emcc-wasi-in-wee8
yy